#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tmf_meta_instance.py 
@version:
@time: 2022/04/08 
"""
from TapClientCore.T02_tm_api.v2_api.v2_tm import v2tm as TM


class TMFMetaTransformerItems:
    field_name = 'field_name'
    data_type = 'data_type'
    javaType = 'javaType'
    columnSize = 'columnSize'
    precision = 'precision'

    @staticmethod
    def _get_metadata_transformer_items(data):
        """
        {
            "where": {
                "dataFlowId": "624d592600424a6608c2ee28",
                "sinkNodeId": "e62b069a-b16d-434f-83e5-0617ffac6b95"
            },
            "limit": 10,
            "skip": 0
        }
        :param data:
        :return:
        """
        return TM.get_metadataTransformerItems(data)

    def get_metadata_transformer_items(self, task_id, sink_id):
        data = {
            "where": {
                "dataFlowId": task_id,
                "sinkNodeId": sink_id
            },
            "limit": 10,
            "skip": 0
        }
        response = self._get_metadata_transformer_items(data)
        items = response.get('data').get('items')[0]
        sourceQualifiedName = items.get('sourceQualifiedName')
        sinkQulifiedName = items.get('sinkQulifiedName')
        invalid = items.get('invalid')
        return {
            'source': sourceQualifiedName,
            'sink': sinkQulifiedName,
            'invalid': invalid
        }

    @staticmethod
    def _get_meta_original_data(data):
        """
        'T_mysql_INSURANCE_B_MYSQL1648192996744839169_623d24bb65a20174d575ae7b'
        :param data:
        :return:
        """
        return TM.get_metadataInstances_originalData(data)

    def get_meta_original_data(self, data):
        response = self._get_meta_original_data(data)
        field = response.get('data')[0].get('fields')
        return field

    @staticmethod
    def _update_meta_instances_update(where, data):
        """
        {"qualified_name": "T_mysql_target_B_MYSQL1648192996744839169_622ace0e8c51e976658dbcf9"}
        {
            "fields": [
                {
                    "autoincrement": "NO",
                    "columnSize": 10,
                    "id": "623d76b578a81b16f4b9a2f9",
                    "oriPrecision": 10,
                    "oriScale": 0,
                    "precision": 10,
                    "scale": 0,
                    "source": "auto",
                    "unique": False,
                    "key": "",
                    "comment": "",
                    "columnPosition": 1,
                    "originalDataType": "INT",
                    "data_code": 4,
                    "data_type": "INT",
                    "dataType": 4,
                    "field_name": "pk",
                    "foreign_key_position": 0,
                    "is_auto_allowed": True,
                    "is_deleted": False,
                    "is_nullable": True,
                    "java_type": "Long",
                    "javaType": "Long",
                    "original_field_name": "pk",
                    "original_java_type": "Long",
                    "primary_key_position": 0,
                    "table_name": "B_MYSQL1648192996744839169",
                    "isPrecisionEdit": True,
                    "isScaleEdit": False
                },
                {
                    "autoincrement": "NO",
                    "columnSize": 255,
                    "id": "623d76b578a81b16f4b9a2fa",
                    "oriPrecision": 255,
                    "precision": 255,
                    "source": "auto",
                    "unique": False,
                    "key": "",
                    "comment": "",
                    "columnPosition": 2,
                    "originalDataType": "VARCHAR",
                    "data_code": 12,
                    "data_type": "VARCHAR",
                    "dataType": 12,
                    "field_name": "title",
                    "foreign_key_position": 0,
                    "is_auto_allowed": True,
                    "is_deleted": False,
                    "is_nullable": True,
                    "java_type": "String",
                    "javaType": "String",
                    "original_field_name": "title",
                    "original_java_type": "String",
                    "primary_key_position": 0,
                    "table_name": "B_MYSQL1648192996744839169",
                    "isPrecisionEdit": True,
                    "isScaleEdit": False
                }
            ]
        }
        :param where:
        :param data:
        :return:
        """
        return TM.post_metadataInstances_update(where, data)

    def get_meta_original_data_by_task_data(self, data):
        task_id = data.get('id')
        sink_id = data.get('dag').get('edges')[0].get('target')
        response = self.get_metadata_transformer_items(task_id, sink_id)
        source_name = response.get('source')
        sink_name = response.get('sink')
        invalid = {'invalid': response.get('invalid')}
        source_model = self.get_meta_original_data(source_name)
        sink_model = self.get_meta_original_data(sink_name)
        source_and_sink_attr = [
            (i, j)
            for i in source_model
            for j in sink_model
            if i[self.field_name] == j[self.field_name]
        ]
        for i, j in source_and_sink_attr:
            if self.precision not in i:
                i[self.precision] = None
            if self.columnSize not in i:
                i[self.columnSize] = None
            if self.javaType not in i:
                i[self.javaType] = None
            if self.data_type not in i:
                i[self.data_type] = None
            if self.precision not in j:
                j[self.precision] = None
            if self.columnSize not in j:
                j[self.columnSize] = None
            if self.javaType not in j:
                j[self.javaType] = None
            if self.data_type not in j:
                j[self.data_type] = None
        res_m_t = [((i[self.field_name], i[self.data_type], i[self.precision], i[self.columnSize], i[self.javaType]),
                    (j[self.field_name], j[self.data_type], j[self.precision], j[self.columnSize], j[self.javaType]))
                   for i, j in source_and_sink_attr]
        res_m_t.append(invalid)
        return res_m_t

    @staticmethod
    def _get_metadataInstances_findInspect(raw_filter):
        id_inq = raw_filter.get('id_inq')
        table_inq = raw_filter.get('table_inq')
        from TapClientCore.T03_api_body_templates.v2_api_body import META_FIND_INSPECT
        filter = META_FIND_INSPECT.copy()
        filter['where']['source.id']['inq'] = id_inq
        filter['where']['original_name']['inq'] = table_inq
        return TM.get_metadataInstances_findInspect(filter)

    def get_metadataInstances_findInspect(self, source_id, table):
        """
        根据数据源id和表名获取 inspect的meta信息
        :param source_id:
        :param table:
        :return:
        """
        data = {
            'id_inq': source_id,
            'table_inq': table
        }
        return self._get_metadataInstances_findInspect(data)

    @staticmethod
    def _post_metadataInstances_findInspect(payload):
        return TM.post_metadataInstances_findInspect(payload)

    def post_metadataInstances_findInspect(self, inq_id, inq_table):
        """
        根据数据源id和表名获取 inspect的meta信息
        """
        data = {"where": {"meta_type": {
            "inq": ["table", "view", "collection", "mongo_view", "hive", "kudu", "dummy db", "gridfs", "file",
                    "elasticsearch", "rest api", "redis", "field_processor", "aggregation_processor", "js_processor",
                    "row_filter_processor", "java_processor", "hive", "hana", "kafka", "dameng", "clickhouse", "kudu",
                    "hbase", "mq", "kafka", "adb_mysql", "tcp_udp", "cache_lookup_processor", "custom_connection",
                    "mem_cache", "logminer", "protobuf_convert_processor", "hazelcast_cloud_cluster"]},
            "source.id": {"inq": inq_id},
            "original_name": {"inq": inq_table}},
            "fields": {"id": True, "name": True, "original_name": True, "source.id": True, "source.name": True,
                       "fields": True, "fields.id": True, "fields.field_name": True,
                       "fields.primary_key_position": True, "databaseId": True, "meta_type": True}}
        return self._post_metadataInstances_findInspect(data)


if __name__ == '__main__':
    t = TMFMetaTransformerItems()
    res = t.get_meta_original_data_by_task_data({
        "id": "623c5e8d65a20174d552d546",
        "customId": "62172cfc49b865ee5379d3ed",
        "lastUpdBy": "62172cfc49b865ee5379d3ed",
        "attrs": {
            "task_data_source_Data": {
                "source_sourceType": "",
                "target_targetType": "",
                "source_filter_databaseType": "oracle",
                "target_filter_databaseType": "mongodb",
                "source_connectionId": "621df3ab98bcdf1ea2e22392",
                "target_connectionId": "62394729decec63c31d0e111",
                "source_connectionName": "oracle_19c_TAPDATA",
                "target_connectionName": "yun_target",
                "source_databaseType": "oracle",
                "target_databaseType": "mongodb",
                "mqType": ""
            },
            "task_setting_Data": {
                "name": "oracle_19c_TAPDATA2yun_targetB_ORA1648112771192875904",
                "sync_type": "initial_sync",
                "distinctWriteType": "intellect",
                "automaticallyCreateTables": True,
                "stopOnError": False,
                "isOpenAutoDDL": False,
                "bidirectional": False,
                "readBatchSize": 1000,
                "cdcConcurrency": False,
                "transformerConcurrency": 8,
                "fieldNameTransform": "",
                "tableNameTransform": "",
                "noPrimaryKey": True,
                "needToCreateIndex": True,
                "existDataProcessMode": "keepData",
                "isSerialMode": False,
                "readShareLogMode": "STREAMING",
                "processorConcurrency": 1,
                "syncPoints": [{
                    "connectionId": "621df3ab98bcdf1ea2e22375",
                    "type": "current",
                    "time": "",
                    "date": "",
                    "name": "",
                    "timezone": "-08:00"
                }],
                "type": "initial_sync"
            },
            "log_collector_save_id": "623c5e8d65a20174d552d548"
        },
        "deduplicWriteMode": "intelligent",
        "increHysteresis": False,
        "increOperationMode": False,
        "increSyncConcurrency": False,
        "processorThreadNum": 8,
        "increaseReadSize": 1,
        "readBatchSize": 1000,
        "isAutoCreateIndex": True,
        "isFilter": False,
        "isOpenAutoDDL": False,
        "isSchedule": False,
        "isStopOnError": False,
        "name": "oracle_19c_TAPDATA2yun_targetB_ORA1648112771192875904",
        "shareCdcEnable": False,
        "statuses": [{
            "id": "623c5e8d65a20174d552d548",
            "status": "edit"
        }],
        "status": "edit",
        "type": "initial_sync",
        "writeThreadSize": 8,
        "editVersion": "1648123533024",
        "syncPoints": [{
            "connectionId": "621df3ab98bcdf1ea2e22375",
            "pointType": "current"
        }],
        "syncType": "migrate",
        "transformProcess": 0,
        "dag": {
            "edges": [{
                "source": "af545801-34c6-4c95-8699-f6a60bb6b173",
                "target": "9fb278b8-84f6-41df-9504-047d109ff75b"
            }],
            "nodes": [{
                "existDataProcessMode": "keepData",
                "fieldProcess": [],
                "connectionId": "621df3ab98bcdf1ea2e22392",
                "databaseType": "oracle",
                "type": "database",
                "catalog": "data",
                "id": "af545801-34c6-4c95-8699-f6a60bb6b173",
                "name": "oracle_19c_TAPDATA",
                "elementType": "Node"
            }, {
                "existDataProcessMode": "keepData",
                "syncObjects": [{
                    "type": "table",
                    "objectNames": ["B_ORA1648112771192875904"]
                }],
                "batchOperationList": [],
                "connectionId": "62394729decec63c31d0e111",
                "databaseType": "mongodb",
                "tablePrefix": "",
                "tableSuffix": "",
                "fieldsNameTransform": "",
                "tableNameTransform": "",
                "type": "database",
                "catalog": "data",
                "id": "9fb278b8-84f6-41df-9504-047d109ff75b",
                "name": "yun_target",
                "elementType": "Node"
            }]
        },
        "shareCache": False,
        "_deleted": False,
        "createTime": "2022-03-24T12:05:33.027+00:00",
        "last_updated": "2022-03-24T12:05:33.088+00:00",
        "user_id": "62172cfc49b865ee5379d3ed"
    })
    print(res)
